<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue 模板</title>
  <script src="https://cdn.bootcss.com/vue/2.6.11/vue.min.js"></script>
  <style>
    .box {
      border: 1px solid red;
      margin-bottom: 50px;
      padding-bottom: 20px;
      padding-left: 10px;
    }

    .tip {
      color: red;
    }
  </style>
</head>

<body>
  <!-- 主控区域 -->
  <div id="app">
    <div class="box">
      <h3>for 循环数组</h3>
      <p>
        <!-- 迭代器 -->
        <li v-for="(item, index)  in items">
          {{index}} - {{ item.message }}
        </li>
      </p>
    </div>

    <div class="box">
      <h3>for 循环对象</h3>
      <p>
        <li v-for="(value, name, index) in object" :key="index">
          {{ index }}. {{ name }}: {{ value }}
        </li>
      </p>
    </div>

    <div class="box">
      <h3>数组修改，监听不到</h3>
      <p>
        <li v-for="item in list">{{item}}</li>
      </p>
      <button @click="change">改变</button>
    </div>

    <div class="box">
      <h3>对象属性的增加删除，监听不到</h3>
      <p>
        <li v-for="(value, name) in listObj">
          {{name}}:{{value}}
        </li>
      </p>
      <button @click="add">增加属性</button>
    </div>


  </div>

  <script>
    var vue = new Vue({
      el: "#app",
      data: {
        items: [
          { message: 'Foo' },
          { message: 'Bar' }
        ],
        object: {
          title: 'How to do lists in Vue',
          author: 'Jane Doe',
          publishedAt: '2016-04-10'
        },
        list: [0,1,2,3,4,5],
        listObj: {
          'first': 1,
          "second": 2
        }
      },
      methods: {
        change: function () {
          console.log('change function')
          // this.list[2] = 9
          this.$set(this.list, 2, 9)
        },
        add: function () {
          console.log('add function')
          // this.listObj.third = 3
          this.$set(this.listObj, 'third', 3)
          console.log(this.listObj)
        }
      }
    })
  </script>
</body>

</html>